﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebTraderLiteCL;
using System.Data;

namespace WebTraderLite.Pages
{
    public partial class AutoSell : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                userStocksSDS.SelectParameters.Add("userName", DbType.String, HttpContext.Current.User.Identity.Name);
                
                if (Request.QueryString["stock"] != null)
                {
                    lstUserStocks.SelectedValue = Request.QueryString["stock"].ToUpper();
                    //lstUserStocks_SelectedIndexChanged(sender, e);
                }
            }
        }
        protected void showConfirmautosell(object sender, EventArgs e)
        {
            //Show confirm autosell view
            MultiViewautosellStock.ActiveViewIndex = 1;

            DBTransactions db = new DBTransactions();

            //Populate number of shares being sold
            TextBox tbNumShares = fvautosellStock.FindControl("tbNumShares") as TextBox;
            Label lblNumShares = fvConfirmautosell.FindControl("lblNumShares") as Label;
            lblNumShares.Text = tbNumShares.Text;

            //Populate purchase price label
            Label lblPurchasePrice = fvConfirmautosell.FindControl("lblPurchasePrice") as Label;
            float purchasePrice = db.retrievePurchasePrice(lstUserStocks.SelectedValue.ToString(), HttpContext.Current.User.Identity.Name);
            lblPurchasePrice.Text = purchasePrice.ToString("C");

            //Populate the auto sale price           
            TextBox tbautoSalePrice = fvautosellStock.FindControl("tbAutoSellPrice") as TextBox;
            Label lblautoSalePrice = fvConfirmautosell.FindControl("lblAutoSellPrice") as Label;
            float autoSalePrice = float.Parse(tbautoSalePrice.Text.Trim());
            lblautoSalePrice.Text = autoSalePrice.ToString("C");


            //Populate users profit (or loss)
            Label lblProfit = fvConfirmautosell.FindControl("lblProfitLoss") as Label;
            float profit = autoSalePrice*float.Parse(lblNumShares.Text.Trim()) - (purchasePrice * float.Parse(lblNumShares.Text));
            lblProfit.Text = profit.ToString("C");


        }

        protected void processSale(object sender, EventArgs e)
        {

            Label lblSymbol = fvConfirmautosell.FindControl("lblStockSym") as Label;
            Stock stk = new Stock(lblSymbol.Text);
            Label lblNumShares = fvConfirmautosell.FindControl("lblNumShares") as Label;
            float numSharesToautosell = float.Parse(lblNumShares.Text);
            TextBox tbautoSalePrice = fvautosellStock.FindControl("tbAutoSellPrice") as TextBox;
            float autoSalePrice = float.Parse(tbautoSalePrice.Text.Trim());
            //Add autosale to database
            AutoTransaction autotrans = new AutoTransaction("AUTOSELL", stk, (Int32)numSharesToautosell, HttpContext.Current.User.Identity.Name,"GREATER THAN", autoSalePrice);
            autotrans.Execute();
            Response.Redirect("~/Pages/Transactions.aspx");
        }

        protected void cancelautosell(object sender, EventArgs e)
        {
            //Redirect back to original buy stock page
            Response.Redirect("~/Pages/AutoSell.aspx");
        }

        protected void lstUserStocks_SelectedIndexChanged(object sender, EventArgs e)
        {
            fvautosellStock.DataBind();
            Label lblSharesOwn = fvautosellStock.FindControl("lblSharesOwned") as Label;
            if ( lstUserStocks.SelectedIndex!= 0)
            {
                DBTransactions db = new DBTransactions();
                string sharesOwned = db.retrieveSharesOwned(lstUserStocks.SelectedValue.ToString(), HttpContext.Current.User.Identity.Name);
                lblSharesOwn.Text = sharesOwned;
            }
        }

    }
}